package com.lottery.im.module.login.ui;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import butterknife.BindView;
import butterknife.OnClick;
import com.google.gson.Gson;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.LoginEntity;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.constant.SpKeyConstant;
import com.lottery.im.module.login.presenter.LoginPresenter;
import com.lottery.im.module.login.view.LoinView;
import com.lottery.im.module.main.ui.HomeFragment;
import com.lottery.im.util.SPUtils;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;

/*
 *
 *  登陆页
 *
 * */
public class LoginFragment extends BaseSwipeFragment<LoginPresenter> implements LoinView {

  @BindView(R.id.et_account) EditText mEtAccount;
  @BindView(R.id.et_pwd) EditText mEtPwd;
  @BindView(R.id.iv_show_pwd) ImageView mIvShowPwd;
  @BindView(R.id.cb_remember_password) CheckBox mCbRememberPassword;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_login;
  }

  @Override protected void initPresenter() {
    presenter = new LoginPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    StatusBarHelper.setStatusBarLightMode(getActivity());
    RxBus.get().register(this);
    mEtAccount.setText((String)SPUtils.get(mActivity,SpKeyConstant.ACCOUNT_KEY,""));
    mEtPwd.setText((String)SPUtils.get(mActivity,SpKeyConstant.PWD_KEY,""));
  }

  @OnClick({
      R.id.iv_show_pwd, R.id.tv_forget_password, R.id.btn_login, R.id.btn_register,
      R.id.tv_msg_login
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_show_pwd:
        // 显示隐藏密码
        showHintPwd();
        break;
      case R.id.tv_forget_password:
        // 忘记密码
        startFragment(ForgetPasswordFragment.create(1));
        break;
      case R.id.btn_login:
        // 登陆
        login();
        break;
      case R.id.btn_register:
        // 注册
        startFragment(new RegisterFragment());
        break;
      case R.id.tv_msg_login:
        // 短信登陆
        startFragment(new LoginFastFragment());
        break;
    }
  }

  /*
   *  显示隐藏密码
   * */
  private void showHintPwd() {
    //密码的显示隐藏，并且保证光标位置不变
    int selectionStart = mEtPwd.getSelectionStart();
    if (mEtPwd.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
      mIvShowPwd.setImageResource(R.drawable.icon_login_hide);
      mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    } else {
      mIvShowPwd.setImageResource(R.drawable.icon_login_show);
      mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
    }
    mEtPwd.setSelection(selectionStart);
  }

  /*
   * 登陆
   * */
  private void login() {
    String account = mEtAccount.getText().toString().trim();
    String pwd = mEtPwd.getText().toString().trim();

    if (TextUtils.isEmpty(account)) {
      showToast(R.string.hint_enter_code);
      return;
    }

    if (TextUtils.isEmpty(pwd)) {
      showToast(R.string.hint_enter_password);
      return;
    }

    hideKeyBoard();
    presenter.login(account, pwd);
  }

  /*
   *   登录成功
   * */
  @Override public void loginSuccess(LoginEntity loginEntity) {

    if (mCbRememberPassword.isChecked()){
      // 记住账号密码
      SPUtils.put(mActivity,SpKeyConstant.ACCOUNT_KEY,mEtAccount.getText().toString());
      SPUtils.put(mActivity,SpKeyConstant.PWD_KEY,mEtPwd.getText().toString());
    }else {
      SPUtils.put(mActivity,SpKeyConstant.ACCOUNT_KEY,"");
      SPUtils.put(mActivity,SpKeyConstant.PWD_KEY,"");
    }

    MyApplication.getInstance().setToken(loginEntity.getToken());
    MyApplication.getInstance().setUserInfo(loginEntity.getUserInfo());
    SPUtils.put(mActivity, SpKeyConstant.TOKEN_KEY, loginEntity.getToken());
    SPUtils.put(mActivity, SpKeyConstant.USERINFO_KEY,
        new Gson().toJson(loginEntity.getUserInfo()));
    RxBus.get().send(RxBusCode.LOGIN_SUCCESS_CODE);
    startFragmentAndDestroyCurrent(new HomeFragment());
  }

  /*
   * 注册成功
   * */
  @Subscribe(code = RxBusCode.REG_SUCCESS_CODE)
  public void regSuccess() {
    popBackStack();
    startFragmentAndDestroyCurrent(new HomeFragment());
  }

  /*
   * 快速登录成功
   * */
  @Subscribe(code = RxBusCode.FAST_LOGIN_SUCCESS_CODE)
  public void fastLogin() {
    popBackStack();
    startFragmentAndDestroyCurrent(new HomeFragment());
  }

  @Override public void onResume() {
    super.onResume();
    StatusBarHelper.setStatusBarLightMode(getActivity());
  }

  @Override protected boolean canDragBack() {
    return false;
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
